package com.ruoyi.gd.ldjl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.gd.ldjl.domain.bo.ZLdjlBo;
import com.ruoyi.gd.ldjl.domain.vo.ZLdjlVo;
import com.ruoyi.gd.ldjl.domain.ZLdjl;
import com.ruoyi.gd.ldjl.mapper.ZLdjlMapper;
import com.ruoyi.gd.ldjl.service.IZLdjlService;

import java.util.*;

/**
 * 录单记录Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-27
 */
@RequiredArgsConstructor
@Service
public class ZLdjlServiceImpl implements IZLdjlService {

    private final ZLdjlMapper baseMapper;

    /**
     * 查询录单记录
     */
    @Override
    public ZLdjlVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询录单记录列表
     */
    @Override
    public TableDataInfo<ZLdjlVo> queryPageList(ZLdjlBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZLdjl> lqw = buildQueryWrapper(bo);
        Page<ZLdjlVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询录单记录列表
     */
    @Override
    public List<ZLdjlVo> queryList(ZLdjlBo bo) {
        LambdaQueryWrapper<ZLdjl> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZLdjl> buildQueryWrapper(ZLdjlBo bo) {
        Map<String, Object> params = bo.getParams();
        // 取消字符串的前后空格
        bo.setZbbh(StringUtils.trimField(bo, "zbbh"));
        bo.setGsmc(StringUtils.trimField(bo, "gsmc"));

        LoginUser loginUser = LoginHelper.getLoginUser();
        LambdaQueryWrapper<ZLdjl> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getZbbh()), ZLdjl::getZbbh, bo.getZbbh());
        lqw.like(StringUtils.isNotBlank(bo.getGsmc()), ZLdjl::getGsmc, bo.getGsmc());
        lqw.eq(bo.getYsjl() != null, ZLdjl::getYsjl, bo.getYsjl());
        lqw.eq(bo.getSjlr() != null, ZLdjl::getSjlr, bo.getSjlr());
        if (bo.getYfStr() != null && bo.getYfStr().length > 0) {
            int[] yfStr = bo.getYfStr();
            List<Integer> list = new ArrayList<Integer>();
            for (int i = 0; i < yfStr.length; i++) {
                list.add(yfStr[i]);
            }
            lqw.in(ZLdjl::getYf, list);
        }
        lqw.like(StringUtils.isNotBlank(bo.getCreateDeptName()), ZLdjl::getCreateDeptName, bo.getCreateDeptName());
        // 管理员可以查看所有的数据
        if(!"admin".equals(loginUser.getUsername())) {
            List<String> strings = Arrays.asList("0");
            lqw.in(ZLdjl::getDelFlag, strings);
            // 如果不是管理员，当前的账号只能查询自己的数据
            bo.setCreateId(loginUser.getUserId()+"");
            lqw.eq(StringUtils.isNotBlank(bo.getCreateId()), ZLdjl::getCreateId, bo.getCreateId());
        }
        return lqw;
    }

    /**
     * 新增录单记录
     */
    @Override
    public Boolean insertByBo(ZLdjlBo bo) {
        ZLdjl add = BeanUtil.toBean(bo, ZLdjl.class);
        LoginUser loginUser = LoginHelper.getLoginUser();
        add.setCreateId(loginUser.getUserId()+"");
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改录单记录
     */
    @Override
    public Boolean updateByBo(ZLdjlBo bo) {
        ZLdjl update = BeanUtil.toBean(bo, ZLdjl.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZLdjl entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除录单记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.batchDeleteIds(ids) > 0;
    }

    @Override
    public List<ZLdjlVo> queryListByIds(String ids) {
        String[] split = ids.split(",");
        List<ZLdjlVo> list = baseMapper.selectByIds(split);
        return list;
    }
}
